from flask import Blueprint,jsonify,request

from utils.db import db1

from comm import get_data

import json

from utils.redis_bp import r

work_bp = Blueprint('work',__name__)

@work_bp.route('/getworkflow')
def getworkflow():
    sql = "select * from workflow"

    res = db1.find_all(sql)

    return jsonify({'code':200,'list':res})



# 获取数据源接口
@work_bp.route('/getresourcedata')
def getresourcedata():
    # 首次先从数据库中取值
    values = r.get_to('resourcedata')

    print(values)

    if values:
        list = eval(values)

    else:
        sql = "select * from data_resource"
        res = db1.find_all(sql)
        list = []
        if res:
            for i in res:
                dict = {'id':i['id'],'name':i['name']}
                sql1 = "select id,name from %s" % (i['tablename'])

                res1 = db1.find_all(sql1)

                dict['mes'] = res1

                list.append(dict)

        r.set_to('dataresource',json.dumps(list))

    return jsonify({'code':200,'list':list})










@work_bp.route('/addworkflow',methods=["POST"])
def addworkflow():
    data = get_data()

    print(data)

    sql = "update workflow set params = '%s' where id = %d" % (data['list'],int(data['id']))

    db1.update(sql)

    return jsonify({'code':200,'mes':'添加成功'})









def zhuanhuan(result1):
    result = []
    dic = {}
    for i in result1:
        if i not in result:
            try:
                if dic[i]:
                    dic[i] += 1
            except:
                dic[i] = 1
    for k, v in dic.items():
        if v % 2 != 0:
            result.append(k)
        else:
            pass
    return result



@work_bp.route('/adddevise',methods=["POST"])
def adddevise():
    data = get_data()

    # sss = zhuanhuan(data['audit'])

    print(data)

    sql = "insert into devise(id,title,params,status,audit,user_id) values(0,'%s','%s',0,'%s',%d)" % (data['title'],data['params'],data['audit'],int(data['userid']))

    db1.update(sql)

    sql = "select id,title,params from devise where title = '%s'" % (data['title'])

    res = db1.find(sql)

    r.insert_hash('getredis',res['title'],res['params'])

    return jsonify({'code':200,'mes':'添加成功'})





def treefirm(data):
    tree = {}
    list = []

    for i in data:
        tree[i['id']] = i
    for j in data:
        pid = j['pid']

        if  not pid:
            list.append(j)   # 添加顶级的父类
        else:
            if "children" not in tree[pid]:
                tree[pid]['children'] = []
                # 查询部门下的所有员工
            sql = "select id as rid,name  from users where dept_id = %d " % (j['id'])

            res = db1.find_all(sql)

            userlist = []

            if res:
                userlist = res
            print(j)
            j['children'] = userlist

            tree[pid]['children'].append(j)

    return list


# def getdeptdata(data):
#     tree = {}
#     list = []
#     for i in data:
#         tree[i['id']] = i
#     for j in data:
#         pid = j['pid']
#         if not pid:
#             list.append(j)
#         else:
#             if 'children' not in tree[pid]:
#                 tree[pid]['children'] = []
#             # 查询此部门下所有员工
#             sql = "select * from users where dept_id=%d" % (j['id'])
#             res = db1.find_all(sql)
#             userlist = []
#             if res:
#                 userlist = res
#             j['children'] = userlist
#             tree[pid]['children'].append(j)
#     return list
@work_bp.route('/getdept')
def getdept():
    sql = "select * from dept"

    res = db1.find_all(sql)

    list1 = treefirm(res)

    return jsonify({'code':200,'list':list1})














